<?php

namespace App\Jobs;

use App\Models\ImFriendsCircle;
use App\Models\ImFriendsCircleImgs;
use App\Models\ImQueueErrorLogs;
use App\Models\ImReport;
use App\Services\Aliyun\Video\VideoServices;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ReportAudit implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data = null;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data = null)
    {
        //
        $this->data = $data;
    }

    /**
     * @desc 队列自动审核
     * @return bool
     * @author zhouzhou
     * @time 2021/12/20 18:34
     */
    public function handle()
    {
        // redis的数据
        $data = $this->data;
        if (!empty($data))
        {
            try
            {
                $reportId = $data['report_id'];
                $imReport = new ImReport();
                $imReportInfo = $imReport->where(['id'=>$reportId])->first();
                $fileAIMediaAuditJob = new VideoServices();
                if ($imReportInfo)
                {
                    $content = $imReportInfo['content'] ?? [];
                    $picture = $imReportInfo['picture_task'] ?? [];
                    $flag = 0;
                    $remark = 'ok';
                    foreach ($content as $key =>&$val)
                    {
                        $type = url_type($val['content']);
                        if ($type == 1)
                        {
                            // 文字审核
                            $data = $fileAIMediaAuditJob->submitTextAIMediaAuditJob($val['content']); //$taskId智能检测任务ID(可获取智能检查结果）
                            $suggestion = $data['data'][0]['results'][0]['suggestion'];
                            if ($suggestion != 'pass')
                            {
                                $flag = 1;
                                $remark = $suggestion;
                            }
                        }
                        else
                        {
                            $res = $fileAIMediaAuditJob->getAIMediaAuditJobInfo($val['task_id'], $type - 1); //$taskId智能检测任务ID(可获取智能检查结果）
                            $result = $res['data'][0]['results'];
                            foreach ($result as $ke =>&$va)
                            {
                                if ($va['suggestion'] != 'pass')
                                {
                                    $flag = 1;
                                    $remark = $va['label'];
                                }
                            }
                        }
                    }

                    foreach ($picture as $k =>&$v)
                    {
                        $res = $fileAIMediaAuditJob->getAIMediaAuditJobInfo($v, 1); //$taskId智能检测任务ID(可获取智能检查结果）
                        $result = $res['data'][0]['results'];
                        foreach ($result as $ke =>&$va)
                        {
                            if ($va['suggestion'] != 'pass')
                            {
                                $flag = 1;
                                $remark = ($va['label'] == 'porn') ? '涉黄' : '涉政';
                            }
                        }
                    }

                    $status = ($flag == 0) ? 3 : 2; // flag 等于0 ，都是正常审核失败，2审核成功有涉黄涉证
                    $imReportInfo->status = $status;
                    $imReportInfo->audit_note = $remark;
                    $imReportInfo->save();
                }
            }catch (\Exception $e)
            {
                $imQueueErrorLogs = new ImQueueErrorLogs();
                $imQueueErrorLogs->insertQueueError(json_encode(['data'=>$data]), 6, $e->getMessage());
            }
        }
    }
}
